import { _decorator, Component, Label } from 'cc';
import { Ab_ClientAPI_bA } from './api/client.api';
const { ccclass, property } = _decorator;

@ccclass('test')
export class test extends Component {

	protected onLoad(){
		this.init();
	}

    protected start() {
		abi.Ab_cc_bA.Ab_updateWidget_bA(this.node);
		abi.Ab_cc_bA.Ab_adaptBackgroundNode_bA(this.node.getChildByName('background'));
    }

	private lbl_info: Label;
	private device_id: string;

	private task_normal_list: __task__[];

	private init(){
		this.lbl_info = this.node.getChildByName('lbl-info').getComponent(Label);

		Ab_ClientAPI_bA.Ab_DeviceID_bA()
		.then(result=>{
			this.device_id = result.Ab_device_id_bA;
			this.node.getChildByName('lbl-device_id').getComponent(Label).string = this.device_id;
		});

		this.task_normal_list = [
			{
				name: 'Ab_device_id_bA',
				call: Ab_ClientAPI_bA.Ab_DeviceID_bA,
				get_params: ()=>[],
				total: 0,
				error: 0
			},{
				name: 'Ab_set_clipboard_bA',
				call: Ab_ClientAPI_bA.Ab_SetClipboard_bA,
				get_params: ()=>[this.device_id],
				total: 0,
				error: 0
			},{
				name: 'Ab_LocalLanguage_bA',
				call: Ab_ClientAPI_bA.Ab_Local_bA,
				get_params: ()=>[],
				total: 0,
				error: 0
			},{
				name: 'Ab_closeLoading_bA',
				call: Ab_ClientAPI_bA.Ab_CloseLoading_bA,
				get_params: ()=>[],
				total: 0,
				error: 0
			},{
				name: 'Ab_type_bA',
				call: Ab_ClientAPI_bA.Ab_GameType_bA,
				get_params: ()=>[],
				total: 0,
				error: 0
			}
		];
	}

	private clickCopyButton(){
		Ab_ClientAPI_bA.Ab_DeviceID_bA()
		.then(result=>Ab_ClientAPI_bA.Ab_SetClipboard_bA(result.Ab_device_id_bA));
	}

	private crash_while: boolean = false;
	private clickCrashButton(){
		if(this.crash_while){
			return void 0;
		}
		this.crash_while = true;

		Promise.resolve('crash-test')
		.then(async()=>{
			while(this.crash_while){
				await delay(20)
				.then(()=>Reflect.set(null, 'crash', 'this is a crash'))
				.catch(e=>console.warn(e));
			}
		});
	}

	private clickOtherCrashButton(){
		this.crash_while = false;

		Reflect.set(null, 'other-crash', 'this is other crash');
	}

	private run_normal: boolean = false;
	private async clickNormalTaskButton(){
		this.run_video = false;
		if(this.run_normal){
			return void 0;
		}
		let task_couunt = this.task_normal_list.length;
		this.run_normal = task_couunt>0;

		Promise.resolve('normal-test')
		.then(async()=>{
			let task: __task__;
			let index = 0;
			while(this.run_normal){
				task = this.task_normal_list[index];
				this.lbl_info.string = '即将测试接口：' + task.name
				+ '\n        总次数：' + task.total + '\n        失败次数：' + task.error;

				await delay(300)
				.then(()=>task.call(...task.get_params()))
				.catch(()=>task.error++)
				.finally(()=>task.total++);

				index = (index+1)%task_couunt;
			}
		});
	}

	private video_count = {
		incentive: {total: 0, error: 0},
		'full-screen': {total: 0, error: 0}
	};
	private run_video: boolean = false;
	private clickVideoTaskButton(){
		this.run_normal = false;
		if(this.run_video){
			return void 0;
		}
		this.run_video = true;

		let video_count = this.video_count;
		let lbl_info = this.lbl_info;
		Promise.resolve('video-test')
		.then(async()=>{
			let ad_type: "incentive" | "full-screen";
			while(this.run_video){
				ad_type = Math.random()>0.5? 'incentive': 'full-screen';
				lbl_info.string = '即将测试广告类型：' + ad_type
				+ '\n播放[incentive]广告\n        总次数：' + video_count.incentive.total + '\n        失败次数：' + video_count.incentive.error
				+ '\n播放[full-screen]广告\n        总次数：' + video_count['full-screen'].total + '\n        失败次数：' + video_count['full-screen'].error;

				await delay(1000)
				.then(()=>Ab_ClientAPI_bA.Ab_AD_bA(ad_type, true))
				.then(success=>success ? void 0 : video_count[ad_type].error++)
				.catch(()=> video_count[ad_type].error++ )
				.finally(()=>video_count[ad_type].total++);
			}
		});
	}
}

type __task__ = {
	name: string;
	call: Function;
	get_params: ()=>any[];
	total: number;
	error: number;
}

function delay(delay: number){
	return new Promise<'delay-end'>((resolve, reject)=>{
		let id = setTimeout(()=>{
			clearTimeout(id);
			resolve('delay-end');
		}, delay);
	});
};
